<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<script type="text/javascript" src="jquery-1.7.js"></script>

<script type="text/javascript">
var timetrid = 0;
function watchtheurl()
{
	
	$.ajax(
		{
			url: "http://www.yihaodian.com/pointProduct/index.do?productId=1114797&isExchange=1",
			async: false,
			dataType: "html",
			complete : function(jqXHR, textStatus)
			{
				//$("#debugShow").html(textStatus);
			},
			error : function(jqXHR, textStatus, errorThrown){
				$("#debugShow").html(textStatus);
			},
			success: function(data, textStatus, jqXHR)
			{
				//$("#debugShow").html(jqXHR.responseText);
				var txt = $(jqXHR.responseText).find("[class='red f24 fb']").first().text();
				timetrid++;
					
				$("#timetried").html(timetrid);
				$("#debugShow").html(txt);	

				if( parseInt(txt) > 100)
				{
					$("#debugShow").append('More Than 100');
					window.setTimeout( function(){ watchtheurl();},3 );
				}
				else
				{	
					
				}
				
				
			}
		}
	);

	
	//window.setTimeout(watchtheurl($url),10);
}





function doOrderStage(tab_id)
{
	$("#debugShow").html(tab_id);
	chrome.tabs.sendRequest(tab_id,{},function(response){});	
}

function doOrder()
{	
	chrome.tabs.create(
		{url:"http://www.yihaodian.com/checkout/index.do"},
		function(tab){	
			//$("#debugShow").html(tab.id);
			doOrderStage(tab.id);  
			//waittabcomplete(tab.id,0);			
		}
	);  		
}


function waittabcomplete(tabid, stage)
{	
	$("#debugShow").append(stage);
	chrome.tabs.get(tabid, function(tab){ 
		$("#debugShow").append(tab.status);
		if(tab.status!="complete")
		{
			window.setTimeout( function(){ waittabcomplete(tabid,stage); },100);
		}
		else{
			chrome.tabs.executeScript(tabid, {file:"b.js"});			
			window.setTimeout( function(){ waittabcomplete(tabid,stage+1); },100);			
		}
	});
}




function trydoExchange(id, buyType)
{
	jQuery.post("http://www.yihaodian.com/product/ajaxAddPointProduct.do",{productID:id,buyType:buyType}
				,function(result){	
					
						
					if( $(result).find("#addProductFailed").length >0 ) // failed
					{
						timetrid++;					
						$("#timetried").html(timetrid);
						$("#debugShow").html(result);			
						window.setTimeout( function(){ trydoExchange(id,buyType);},3 );
					}
					else
					{
						doOrder();
					}
					
    	});
}

</script>

<body>
<div id = 'timetried' ></div>
<p></p>
<div id = 'debugShow' >
</div>


<input name="productid" type=""  />

<input name="clickbtn" type="button" onclick="doOrder()" />


</body>
</html>
